//
//  AdviceViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/14.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "AdviceViewController.h"
#import "ComplainAndAdviceStore.h"

#define totalLength 512

@interface AdviceViewController ()<UITextViewDelegate>
{
    
    __weak IBOutlet UILabel *la;
    __weak IBOutlet UITextView *tw;
}
@end

@implementation AdviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    tw.delegate = self;
    
    [self setNavBarWithTitle:@"投诉和建议"];
}

- (void)textViewDidChange:(UITextView *)textView{
    
    if (textView.text.length > 0) {
        
        la.alpha = 0;
    }else{
        
        la.alpha = 1;
    }
}

#pragma mark - 提交
- (IBAction)onPostBtn:(UIButton *)sender {
    
    [self.view endEditing:YES];
    
    if (tw.text.length <= 0) {
        
        return [self showMBPError:@"请输入您的反馈意见"];
    }
    ComplainAndAdviceStore *store = [[ComplainAndAdviceStore alloc] init];
    
    MJWeakSelf
    [store postAdviceWithCustomId:[UserDefaultsManager sharedInstance].user_id andContent:tw.text Success:^{
        
        [weakSelf showMBPError:@"提交成功!"];
        
        self->tw.text = @"";
        self->la.alpha = 1;
    } Failure:^(NSError *error) {
        
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
    if (str.length > totalLength){
        NSRange rangeIndex = [str rangeOfComposedCharacterSequenceAtIndex:totalLength];
        if (rangeIndex.length == 1){
            textView.text = [str substringToIndex:totalLength];
            // 计数的label
//            self.totalLabel.text = [NSString stringWithFormat:@"%lu/%ld", (unsigned long)textView.text.length, (long)totalLength];
//            [RDWindowHudService showViewWithInfo:@"字数不得超过500"];
        }else{
            NSRange rangeRange = [str rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, totalLength)];
            textView.text = [str substringWithRange:rangeRange];
        }
        return NO;
    }
    return YES;
}


@end
